package com.sys.kafka.factory;

import com.sys.kafka.core.consumer.ConsumerHandler;
import com.sys.kafka.core.consumer.IConsumerHandler;
import com.sys.midware.logger.Logger;
import com.sys.midware.logger.LoggerFactory;

/**
 * 获取默认的消息生产实力 <b>ClassName：</b> ProducerFactory <br/>
 * <b>Description：</b> TODO <br/>
 * <b>@author：</b> mobing <br/>
 * <b>@date：</b> 2015年10月19日 下午2:54:26 <br/>
 * <b>@version: </b> <br/>
 */
public class ConsumerFactory {
    private static final Logger logger = LoggerFactory.getLogger(ConsumerFactory.class);

    /**
     * IProducer 接口定义
     */
    private static IConsumerHandler consumerHandler = null;

    /**
     * 获取默认消息生产实例
     * @return IProducer
     */
    public static IConsumerHandler getInstance() {
        return getInstance(null,null);
    }
    /**
     * 获取消息生产实例
     * @param producerGroup
     *            消息发送的组名
     * @return IProducer
     */
    public static IConsumerHandler getInstance(String clientId,String producerGroup) {
        if (null != consumerHandler) {
            return consumerHandler;
        }
        
        synchronized (ConsumerFactory.class) {
            if (null == consumerHandler) {
                consumerHandler = new ConsumerHandler();
                
                Runtime.getRuntime().addShutdownHook(new CleanHook());
            }
        }
        return consumerHandler;
    }
    
    static void clean() {
        try {
//            producerHandler.shutdown();
        } catch (Exception e) {
            logger.error("clean producer error!", e);
        }
    }

    static class CleanHook extends Thread {
        public void run() {
            clean();
        }
    }
}
